/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.views;

import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import pe.edu.pucp.Servicio.Servicio_ServiciosTerminal;
import pe.edu.pucp.bean.Servicio;

/**
 *
 * @author LiziTa
 */
public class PanelServicioEditar extends javax.swing.JPanel {
Servicio_ServiciosTerminal service = Servicio_ServiciosTerminal.get_instance();
     public  int variable;
     String cad=null;
    ServicioTM miservicio = new ServicioTM();
     
    
        
   public void actualizaTabla() {
     
       miservicio.services=service.queryAllServicios();
       miservicio.fireTableChanged(null);
        
    }
    
    
     private void groupButton( ) {

    ButtonGroup bg1 = new ButtonGroup( );

    bg1.add(rbtnSoles);
    bg1.add(rbtnDolares);   
    }
     
      class ServicioTM extends AbstractTableModel{
         
        
        	
   
          
    String titulos[]={"CODIGO","DESCRIPCION","PRECIO BASE","UNIDAD COBRO"};
      private  ArrayList<Servicio> services = service.queryAllServicios();
         
        @Override
        public int getRowCount() {
           return services.size();
        }

        @Override
        public int getColumnCount() {
            return 4;
        }

        @Override
        public Object getValueAt(int fil, int col) {//COLOCO LOS VALORES EN LA TABLA
            String res=null;
            Servicio s=services.get(fil);
            switch(col){
                case 0:
                    res = "" + s.getId();
                    break;
                case 1:
                    res = ""+s.getDescripcion();
                    break;
                case 2:
                    res = ""+s.getPreciobase();
                    break;
                                               
                case 3: 
                    res = ""+s.getUnidadcobro();
                     break;               
            }
            return res;
        }
       @Override  
        public String getColumnName(int col){
        return titulos[col];
        }
      
      }
    public PanelServicioEditar() {
        initComponents();
        TablaServicio.setModel(miservicio);
        groupButton();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        txtDescripcion = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        rbtnSoles = new javax.swing.JRadioButton();
        rbtnDolares = new javax.swing.JRadioButton();
        jLabel13 = new javax.swing.JLabel();
        txtPbase = new javax.swing.JFormattedTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        TablaServicio = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        Editar = new javax.swing.JButton();
        Cancelar = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Datos del Servicio"));
        jPanel1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jPanel1KeyReleased(evt);
            }
        });

        jLabel2.setText("Descripcion:");

        jLabel3.setText("Precio base:");

        jLabel1.setText("Código:");

        jLabel4.setText("Unidad cobro");

        txtId.setEnabled(false);
        txtId.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIdActionPerformed(evt);
            }
        });
        txtId.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtIdKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtIdKeyReleased(evt);
            }
        });

        txtDescripcion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDescripcionActionPerformed(evt);
            }
        });
        txtDescripcion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtDescripcionKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtDescripcionKeyTyped(evt);
            }
        });

        rbtnSoles.setText("Soles");
        rbtnSoles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnSolesActionPerformed(evt);
            }
        });

        rbtnDolares.setText("Dolares");
        rbtnDolares.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnDolaresActionPerformed(evt);
            }
        });

        jLabel13.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/pucp/views/logo.gif"))); // NOI18N

        txtPbase.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtPbaseKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(12, 12, 12)
                                .addComponent(jLabel6))
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtId, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
                        .addGap(61, 61, 61)
                        .addComponent(jLabel13))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtPbase, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(txtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addGap(18, 18, 18)
                                    .addComponent(rbtnSoles)
                                    .addGap(18, 18, 18)
                                    .addComponent(rbtnDolares))))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtPbase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(15, 15, 15)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(rbtnSoles)
                    .addComponent(rbtnDolares))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jLabel6))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))))
                .addContainerGap())
        );

        TablaServicio.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        TablaServicio.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TablaServicioMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(TablaServicio);

        jLabel5.setText("Tabla de Servicios Tramarsa");

        Editar.setText("Editar");
        Editar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditarActionPerformed(evt);
            }
        });

        Cancelar.setText("Cancelar");
        Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel5))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(139, 139, 139)
                        .addComponent(Editar, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(39, 39, 39)
                        .addComponent(Cancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 488, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Editar)
                    .addComponent(Cancelar))
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(32, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txtIdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIdActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtIdActionPerformed

    private void txtIdKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtIdKeyPressed

    }//GEN-LAST:event_txtIdKeyPressed

    private void txtIdKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtIdKeyReleased
        if (!txtId.getText().matches("[0-9]*")) {
            JOptionPane.showMessageDialog(null, "Dato del Id no valido", "Error!", JOptionPane.ERROR_MESSAGE);
            txtId.setText("");
        }

        if (txtId.getText().matches("[0-9]*")) {
            String str = txtId.getText();
            int tam = str.length();
            if (tam > 8) {
                JOptionPane.showMessageDialog(null, "El ID permite máximo 8 caracteres", "Error!", JOptionPane.ERROR_MESSAGE);
                txtId.setText("");
            }
        }
    }//GEN-LAST:event_txtIdKeyReleased

    private void txtDescripcionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDescripcionActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtDescripcionActionPerformed

    private void txtDescripcionKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDescripcionKeyReleased
        if (txtDescripcion.getText().matches("[a-z0-9A-Z]*")) {
            String str = txtDescripcion.getText();
            int tam = str.length();
            if (tam > 50) {
                JOptionPane.showMessageDialog(null, "La descripcion no debe exceder de 50 caracteres", "Error!", JOptionPane.ERROR_MESSAGE);

            }

        }
    }//GEN-LAST:event_txtDescripcionKeyReleased

    private void rbtnSolesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnSolesActionPerformed
        variable = 0;
        rbtnSoles.setVisible(true);

        // soles      
    }//GEN-LAST:event_rbtnSolesActionPerformed

    private void rbtnDolaresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnDolaresActionPerformed
        variable = 1; // dolares
    }//GEN-LAST:event_rbtnDolaresActionPerformed

    private void jPanel1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPanel1KeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_jPanel1KeyReleased

    private void EditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditarActionPerformed
    Servicio s = new Servicio();
        s.setId(txtId.getText());
        s.setDescripcion(txtDescripcion.getText());
        s.setPreciobase(Float.parseFloat(txtPbase.getText()));
        service.editaServicio(s);
        actualizaTabla();
       
    }//GEN-LAST:event_EditarActionPerformed

    private void CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_CancelarActionPerformed

    private void TablaServicioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TablaServicioMouseClicked
      //variable=0;
         int rowSel = TablaServicio.getSelectedRow();
        String idServicio=TablaServicio.getValueAt(rowSel,0).toString();
        String tipo=TablaServicio.getValueAt(rowSel,3).toString();
        Servicio serv=service.queryByServicio(idServicio);
 
        txtId.setText(idServicio);
        txtDescripcion.setText(serv.getDescripcion());
        txtPbase.setText(Float.toString(serv.getPreciobase()));
       
            if (tipo.equals("Soles"))  {
        rbtnSoles.setSelected(true);
        rbtnDolares.setSelected(false);
          }
           else if(tipo.equals("Dolares"))   {        
                rbtnDolares.setSelected(true);   
                rbtnSoles.setSelected(false);
           }
            
    }//GEN-LAST:event_TablaServicioMouseClicked

    private void txtPbaseKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPbaseKeyTyped
char a=evt.getKeyChar();
String texto;
//javax.swing.JTextField jTexto = new javax.swing.JTextField();
if ( "txtPbase".equals(evt.getComponent().getName()))
texto=txtPbase.getText().toString();
else
texto=txtPbase.getText().toString();
if ((!(a>=KeyEvent.VK_0 && a<=KeyEvent.VK_9)) && (!(a==KeyEvent.VK_PERIOD))) {
evt.consume();
}
if (a==KeyEvent.VK_PERIOD) {
if (texto.indexOf(".")>0) evt.consume();

}
 if (texto.length()>9) evt.consume();  
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPbaseKeyTyped

    private void txtDescripcionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDescripcionKeyTyped
  char car = evt.getKeyChar();    
if((car<'a' || car>'z') && (car<'A' || car>'Z') 
   && (car!=(char)KeyEvent.VK_SPACE))
{
   evt.consume();
}       
if(txtDescripcion.getText().length()>=50) evt.consume();  
    }//GEN-LAST:event_txtDescripcionKeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Cancelar;
    private javax.swing.JButton Editar;
    private javax.swing.JTable TablaServicio;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton rbtnDolares;
    private javax.swing.JRadioButton rbtnSoles;
    private javax.swing.JTextField txtDescripcion;
    private javax.swing.JTextField txtId;
    private javax.swing.JFormattedTextField txtPbase;
    // End of variables declaration//GEN-END:variables
}
